<?
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
	set_time_limit(60);
	include('./class/PHPExcel.php');
	$PHPExcel = new PHPExcel();
	$Excel = array();
	if(!isset($filename)){
		$filename=date('YmdHis').'买到产品订单列表';
		if($_REQUEST['atype']=='sale') $filename=date('YmdHis').'卖出产品订单列表';
	}
	$fieldDname = '供应商';
	if($_REQUEST['atype']=='sale') $fieldDname = '分销商';
	$Excel[0]['UUordernum']='订单编号';
	$Excel[0]['UUordertime']='下单时间';
	$Excel[0]['UUbegintime']='游玩时间';
	$Excel[0]['UUdtime']='完成时间';
	$Excel[0]['UUpaystatus']='支付状态';
	$Excel[0]['cancel']='取消时间';
	$Excel[0]['UUdname']=$fieldDname;
	$Excel[0]['UUordername']='取票人';
	$Excel[0]['UUordertel']='手机';
	$Excel[0]['UUp_type']='类别';
	$Excel[0]['UUltitle']='产品名称';
	$Excel[0]['UUtprice']='单价(元)';
	$Excel[0]['UUtnum']='门票数量';
	$Excel[0]['UUpmode']='支付方式';
	$Excel[0]['UUstatus']='订单状态';
	$Excel[0]['totalPrice']='总金额(元)';
	$Excel[0]['UUmemo']='备注';
	$Excel[0]['UUremotenum']='远端订单号（防止科学记数法，订单前面加有 No: 字样）';
	$Excel[0]['UUcode']='凭证码';
	$Excel[0]['UUbuyid']='用户ID';
	$Excel[0]['a']='';
	$Excel[0]['b']='';
	$Excel[0]['c']='';
	

	$i=1;
    // print_r($r);exit;
	foreach($r as $lid=>$row){
		$n++;
		$Excel[$n]['UUordernum']=$row['mainOrder']->UUordernum;
		$Excel[$n]['UUordertime']=$row['mainOrder']->UUordertime;
		$Excel[$n]['UUbegintime']=$row['mainOrder']->UUbegintime;
		$Excel[$n]['UUdtime']=$row['mainOrder']->UUdtime!="0000-00-00 00:00:00"?(string)$row['mainOrder']->UUdtime:"未验证";
		if($row['mainOrder']->UUpaystatus==1){
			$Excel[$n]['UUpaystatus'] = '已支付';
		}elseif($row['mainOrder']->UUpaystatus==2){
			$Excel[$n]['UUpaystatus'] = '未支付';
		}else{
			$Excel[$n]['UUpaystatus'] = '景区到付';
		}
		$Excel[$n]['cancel']=$row['mainOrder']->UUctime;
		
		if($_REQUEST['atype']=='buy'){
			$Excel[$n]['UUdname']=$anames[(string)$row['mainOrder']->UUaid];
		}else{
			$Excel[$n]['UUdname']=$fnames[(int)$row['mainOrder']->UUbuyid]?$fnames[(int)$row['mainOrder']->UUbuyid]:(string)$row['mainOrder']->UUdname;
		}
		
		$Excel[$n]['UUordername']=(string)$row['mainOrder']->UUordername;
		$Excel[$n]['UUordertel']=(string)$row['mainOrder']->UUordertel;
		$Excel[$n]['UUp_type']=OrderRecord::$p_typeT[(string)$row['mainOrder']->UUp_type];
		$Excel[$n]['UUltitle']=(string)$row['mainOrder']->UUltitle.(string)$row['mainOrder']->UUttitle;
		$Excel[$n]['UUtprice']=(string)$row['mainOrder']->UUtprice/100;
		$Excel[$n]['UUtnum']=(string)$row['mainOrder']->UUtnum;
		$Excel[$n]['UUpmode']=OrderRecord::$paymodeT[(string)$row['mainOrder']->UUpmode];
		$Excel[$n]['UUstatus']=OrderRecord::$_nSStatusT[(string)$row['mainOrder']->UUstatus];
        if(((string)$row['mainOrder']->UUstatus==2 && (string)$row['mainOrder']->UUpaystatus==2)){
            $Excel[$n]['UUstatus'].="(未支付)";
        }
		$Excel[$n]['totalPrice']=(string)$row['mainOrder']->UUtnum * (string)$row['mainOrder']->UUtprice/100;
		$Excel[$n]['UUmemo']=(string)$row['mainOrder']->UUmemo;
		$Excel[$n]['UUremotenum']='No:'.(string)$row['mainOrder']->UUremotenum;
        //|| $row['mainOrder']->UUlid==10701
		$Excel[$n]['UUcode'] = ($row['mainOrder']->UUstatus==1 ) ? $row['mainOrder']->UUcode:'';
		$Excel[$n]['UUbuyid'] = (int)$row['mainOrder']->UUmid;
		
		if($row['mainOrder']->UUp_type=='H'){
			$series = (string)$row['mainOrder']->UUseries;
			$series = unserialize($series);
			$arr = explode('，', $series[6]);
			$round = explode('：', $arr[1]);
			$Excel[$n]['a'] = '演出时间：'.$series[4];
			$Excel[$n]['b'] = '座位分区：'.$round[1];
			$Excel[$n]['c'] = '座位号：'.str_replace('_', ',', $series[5]);
		}
		
		
		//从订单
		if($row['childOrder']){
            foreach($row['childOrder'] as $child){
                $n++;
                $Excel[$n]['UUordernum']=$child['mainOrder']->UUordernum;
                $Excel[$n]['UUordertime']=$child['mainOrder']->UUordertime;
                $Excel[$n]['UUbegintime']=$child['mainOrder']->UUbegintime;
                $Excel[$n]['UUdtime']=$child['mainOrder']->UUdtime!="0000-00-00 00:00:00"?(string)$child['mainOrder']->UUdtime:"未验证";
				
				if($child['mainOrder']->UUpaystatus==1){
					$Excel[$n]['UUpaystatus'] = '已支付';
				}elseif($row['mainOrder']->UUpaystatus==2){
					$Excel[$n]['UUpaystatus'] = '未支付';
				}else{
					$Excel[$n]['UUpaystatus'] = '景区到付';
				}
				$Excel[$n]['cancel']=$child['mainOrder']->UUctime;
                if($_REQUEST['atype']=='buy'){
                    $Excel[$n]['UUdname']=$anames[(string)$arr->UUaid];
                }else{
                    $Excel[$n]['UUdname']=$fnames[(int)$arr->UUbuyid]?$fnames[(int)$arr->UUbuyid]:(string)$arr->UUdname;
                }
                $Excel[$n]['UUordername']=(string)$child['mainOrder']->UUordername;
                $Excel[$n]['UUordertel']=(string)$child['mainOrder']->UUordertel;
                $Excel[$n]['UUp_type']=OrderRecord::$p_typeT[(string)$child['mainOrder']->UUp_type];
                $Excel[$n]['UUltitle']=(string)$child['mainOrder']->UUltitle.(string)$child['mainOrder']->UUttitle;
                $Excel[$n]['UUtprice']=(string)$child['mainOrder']->UUtprice/100;
                $Excel[$n]['UUtnum']=(string)$child['mainOrder']->UUtnum;
                $Excel[$n]['UUpmode']=OrderRecord::$paymodeT[(string)$child['mainOrder']->UUpmode];
                $Excel[$n]['UUstatus']=OrderRecord::$_nSStatusT[(string)$child['mainOrder']->UUstatus];
                if(((string)$child['mainOrder']->UUstatus==2 && (string)$child['mainOrder']->UUpaystatus==2)){
                    $Excel[$n]['UUstatus'].="(未支付)";
                }
                $Excel[$n]['totalPrice']=(string)$child['totalPrice']/100;
                $Excel[$n]['UUmemo']=(string)$child['mainOrder']->UUmemo;
                $Excel[$n]['UUremotenum']=(string)$child['mainOrder']->UUremotenum;
				$Excel[$n]['UUcode'] = ((string)$child['mainOrder']->UUstatus==1) ? $child['mainOrder']->UUcode:'';
				$Excel[$n]['UUbuyid'] = (int)$child['mainOrder']->UUmid;
				if($child['mainOrder']->UUp_type=='H'){
					$series = (string)$child['mainOrder']->UUseries;
					$series = unserialize($series);
					$arr = explode('，', $series[6]);
					$round = explode('：', $arr[1]);
					$Excel[$n]['a'] = '演出时间：'.$series[4];
					$Excel[$n]['b'] = '座位分区：'.$round[1];
					$Excel[$n]['c'] = '座位号：'.str_replace('_', ',', $series[5]);
				}
				
            }
		}
	}

	foreach ($Excel as $i => $row) {
		$j = $i+1;
		$PHPExcel->getActiveSheet()->setCellValue('A' . $j, $row['UUordernum']);
		$PHPExcel->getActiveSheet()->setCellValue('B' . $j, $row['UUordertime']);
		$PHPExcel->getActiveSheet()->setCellValue('C' . $j, $row['UUbegintime']);
		$PHPExcel->getActiveSheet()->setCellValue('D' . $j, $row['UUdtime']);
		$PHPExcel->getActiveSheet()->setCellValue('E' . $j, $row['UUpaystatus']);
		$PHPExcel->getActiveSheet()->setCellValue('F' . $j, $row['cancel']);
		$PHPExcel->getActiveSheet()->setCellValue('G' . $j, $row['UUdname']);
		$PHPExcel->getActiveSheet()->setCellValue('H' . $j, $row['UUordername']);
		$PHPExcel->getActiveSheet()->setCellValue('I' . $j, $row['UUordertel']);
		$PHPExcel->getActiveSheet()->setCellValue('J' . $j, $row['UUp_type']);
		$PHPExcel->getActiveSheet()->setCellValue('K' . $j, $row['UUltitle']);
	 	$PHPExcel->getActiveSheet()->setCellValue('L' . $j, $row['UUtprice']);
		$PHPExcel->getActiveSheet()->setCellValue('M' . $j, $row['UUtnum']);
		$PHPExcel->getActiveSheet()->setCellValue('N' . $j, $row['UUpmode']);
		$PHPExcel->getActiveSheet()->setCellValue('O' . $j, $row['UUstatus']);
		$PHPExcel->getActiveSheet()->setCellValue('P' . $j, $row['totalPrice']);
		$PHPExcel->getActiveSheet()->setCellValue('Q' . $j, $row['UUmemo']);
		$PHPExcel->getActiveSheet()->setCellValue('R' . $j, $row['UUremotenum']);
		$PHPExcel->getActiveSheet()->setCellValue('S' . $j, $row['UUcode']);
		$PHPExcel->getActiveSheet()->setCellValue('T' . $j, $row['UUbuyid']);
		// $PHPExcel->getActiveSheet()->setCellValue('W' . $j, $row['a']);
		// $PHPExcel->getActiveSheet()->setCellValue('X' . $j, $row['b']);
		// $PHPExcel->getActiveSheet()->setCellValue('Y' . $j, $row['c']);
	}
	$filename = urlencode($filename . '.xls');
	$filename = str_replace("+", "%20", $filename);
	// echo aaa;die;
	// header('Pragma:public');
	// header('Content-Type:application/x-msexecl;name="'.$filename.'".xls');
	// header('Content-Disposition:inline;filename="'.$filename.'".xls');

	// Redirect output to a client’s web browser (Excel5)
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="'.$filename);
	header('Cache-Control: max-age=0');
	// If you're serving to IE 9, then the following may be needed
	header('Cache-Control: max-age=1');
	// If you're serving to IE over SSL, then the following may be needed
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0
		// $xls = new SimpleExcel('UTF-8', true,'订单列表');
  	//   $xls->addArray($Excel);
  	//   $xls->generateXML($filename);
  	// $PHPExcel->createSheet();
  	$objWriter = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');
	$objWriter->save('php://output');
	exit;
}
?>